<template>
  <div>
    <header>
      <van-nav-bar
        title="我的购物车"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
      />
    </header>
    <main class="card">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="item in goodspicture" :key="item.id"
          ><img :src="item.path" alt=""
        /></van-swipe-item>

        <template #indicator="{ active, total }">
          <div class="custom-indicator">{{ active + 1 }}/{{ total }}</div>
        </template>
      </van-swipe>
      <div class="text">
        <div class="price">
          <p>
            <strong>￥</strong> <span class="gui">{{ goodsData.price }}</span>
            <span>.00</span>
            <!-- <span>库存:{{ goodsData.stock_num }}</span> -->
          </p>
        </div>
        <div class="goodsName">
          <p>{{ goodsData.name }}</p>
        </div>
        <!-- <div class="goodsDesc">{{ goodsData.desc }}</div> -->
      </div>

      <div class="photos" v-for="item in goodspicture" :key="item.id">
        <img :src="item.path" alt="" />
      </div>
    </main>

    <van-action-bar>
      <!-- <van-action-bar-icon icon="chat-o" text="客服" color="#ee0a24" /> -->
      <van-action-bar-icon icon="cart-o" text="购物车" to="/cart" :badge="b" />
      <!-- <van-action-bar-icon icon="star" text="已收藏" color="#ff5000" /> -->
      <van-action-bar-button
        type="warning"
        text="加入购物车"
        @click="addCart"
      />
      <van-action-bar-button type="danger" text="立即购买" />
    </van-action-bar>
  </div>
</template>

<script setup>
import { Toast } from "vant";
import { useRoute } from "vue-router";
import { ref, reactive, defineProps, computed } from "vue";
import { get, post, postToken, getCart } from "../utils/request";
let params = reactive({ project_id: 248, page: 1, limit: 10 });
let goodsData = ref([]);
let goodspicture = ref([]);
console.log(goodspicture);

defineProps({
  goodsData: Array,
});

let route = useRoute();
// console.log(route);
// console.log(route.query);
// console.log(goodsData);
// 请求后台数据
const getDatas = async () => {
  let res = await get("/goods/" + route.query.id);
  console.log(res);
  goodsData.value = res.result;
  goodspicture.value = res.result.s_goods_photos;
  console.log(goodsData.value);
};
getDatas();
// 请求购物车列表数量
let cartAllLists = ref([]);
const getCartLists = async () => {
  let res = await get(
    `/shoppingCart?project_id=248`,
    sessionStorage.getItem("token")
  );
  console.log(res);
  cartAllLists.value = res.result;
};
getCartLists();
// 定义购物车右上角数字
let b = computed(() => {
  return cartAllLists.value.length;
});
// 路由传参
const addCart = async () => {
  let res;
  try {
    res = await postToken("/shoppingCart", sessionStorage.getItem("token"), {
      goods_id: goodsData.value.id,
      num: 1,
      project_id: 248,
    });
    Toast("商品添加成功");
    getCartLists();
  } catch (error) {
    Toast("商品添加失败");
  }
};

// 顶部导航栏
const onClickLeft = () => history.back();
</script>

<style lang="scss" scoped>
header {
  width: 100%;
  height: 0.4rem;
  background-color: skyblue;
  .van-nav-bar__content {
    height: 40px;
  }
}

main {
  width: 100%;
  height: 1.5rem;
  background-color: green;
  // 轮播图
  .my-swipe .van-swipe-item {
    height: 3.5rem;
    color: #fff;
    font-size: 20px;
    line-height: 150px;
    text-align: center;
    background-color: #39a9ed;
  }
  .custom-indicator {
    position: absolute;
    right: 5px;
    bottom: 5px;
    padding: 2px 5px;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.1);
    color: #fff;
  }

  .text {
    width: 100%;
    height: 1.6rem;
    .price {
      width: 100%;
      height: 0.4rem;
      p {
        margin-left: 10px;
        font-size: 18px;
        color: #f2270c;
        .gui {
          font-size: 30px;
          font-family: JDZH-Regular;
        }
      }
    }
    .goodsName {
      padding: 0.1rem;
      color: #262626;
      font-size: 16px;
      font-weight: 700;
    }
  }
}

.van-action-bar .van-safe-area-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 0.5rem;
  background-color: #fff;
  display: flex;
  align-items: center;
  margin-bottom: 0.5rem;
  z-index: 99;
}
</style>
